import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Hangmanbile extends MIDlet implements CommandListener {

	private Display display;
	private Form panel;
	private ChoiceGroup level;
	private Command startCommand;
	private Command confirmCommand;
	private Image[] pics;
	private TextField inputLetter;
	private ImageItem hangImage;
	private LetterString chosenLetters;
	private LetterString hiddenWord;
	private byte errors;
	private final String[][] words = {{"UVA","CAJU","PAI","RAIZ","CASA"}, // 3 or 4
									  {"CAVALO","CARDUME","TRIBUNA","SAPATO","DECIDIR"}, // 6 or 7
									  {"PERNAMBUCO","COMPUTADOR","REPIMBOCA","PARAFUSETA","FOTOGRAFIA"}}; // 9 or 10
	
	public Hangmanbile() {
		super();
		
		this.display = Display.getDisplay(this);
		this.panel = new Form("Hangmanbile - options");

		this.level = new ChoiceGroup("Vocabulary level:", Choice.EXCLUSIVE);
		this.level.append("easy", null);
		this.level.append("medium", null);
		this.level.append("hard", null);
		this.level.setSelectedIndex(1, true);
		
		startCommand = new Command("Start game", Command.SCREEN, 0);
		Command e = new Command("Exit", Command.EXIT, 0);
		
		this.panel.append(this.level);
		this.panel.addCommand(startCommand);
		this.panel.addCommand(e);
		
		this.panel.setCommandListener(this);
	}

	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(this.panel);
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	public void commandAction(Command c, Displayable d) {
		if (c.getLabel().equals("Exit")) {
			try {
				destroyApp(true);
			} catch (MIDletStateChangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			notifyDestroyed();
		}
		else if (c.getLabel().equals("Start game")) {
			this.startGame(level.getSelectedIndex());
		}
		else if (c.getLabel().equals("Confirm")) {
			this.panel.removeCommand(this.confirmCommand);
			
			if (this.inputLetter.getString().trim().equals("")) {
				this.inputLetter.setString("");
				this.panel.addCommand(this.confirmCommand);
				return;
			}
			
			char letter = this.inputLetter.getString().trim().charAt(0);
			
			if (!this.chosenLetters.addLetter(letter)) { // new letter
				
				if (this.hiddenWord.testLetter(letter)) { // wrong answer
					this.hangImage.setImage(this.pics[++this.errors]);
					
					if (this.errors == 6) {
						this.panel.append(new StringItem("Game over", "Try again."));
					} else {
						this.panel.addCommand(this.confirmCommand);
					}
				}
				else { // right answer
					if (!this.hiddenWord.hidden()) {
						this.panel.append(new StringItem("Game over", "Congratulations!"));
					} else {
						this.panel.addCommand(this.confirmCommand);
					}
				}
			} else {
				this.panel.addCommand(this.confirmCommand);
			}
			
			this.inputLetter.setString("");
			this.display.setCurrentItem(this.inputLetter);
		}
	}

	private void startGame(int level) {
		this.errors = 0;
		this.panel.setTitle("Hangmanbile");
		this.panel.removeCommand(startCommand);
		
		this.pics = new Image[7];
		try {
			for (byte i = 0; i < this.pics.length; i++) {
				this.pics[i] = Image.createImage("/pic" + (i + 1) + ".png");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.hangImage = new ImageItem(null, this.pics[errors], ImageItem.LAYOUT_DEFAULT, null);
		this.panel.delete(0);
		this.panel.append(this.hangImage);

		Random r = new Random();
		String word = this.words[level][r.nextInt(this.words[level].length)];
		
		this.hiddenWord = new LetterString("Hidden word:", word);
		this.panel.append(this.hiddenWord);
		
		this.inputLetter = new TextField("Choose a letter: ", "", 1, TextField.ANY);
		this.panel.append(this.inputLetter);
		
		this.chosenLetters = new LetterString("Chosen letters:", "");
		this.panel.append(this.chosenLetters);
		
		this.confirmCommand = new Command("Confirm", Command.SCREEN, 0);
		this.panel.addCommand(this.confirmCommand);
		
		this.display.setCurrentItem(this.inputLetter);
	}
}